package javamobile.billsplit;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javamobile.utils.MySocket;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class RegisterActivity extends Activity {

	private Button register;
	private EditText password;
	private EditText username;
	private EditText firstname;
	private EditText lastname;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        register = (Button) findViewById(R.id.register_REGISTER);
        password = (EditText) findViewById(R.id.password_REGISTER);
        username = (EditText) findViewById(R.id.username_REGISTER);
        firstname = (EditText) findViewById(R.id.firstname_REGISTER);
        lastname = (EditText) findViewById(R.id.lastname_REGISTER);
    

        register.setOnClickListener(new OnClickListener() {
        	@SuppressWarnings("unchecked")
			@Override
        	public void onClick(View v) {
        		Intent toMain = new Intent(RegisterActivity.this, MainActivity.class);

        	    Map<String, Object> input = new HashMap<String, Object>();
                String password_str = password.getText().toString();
                String username_str = username.getText().toString();
                String firstname_str = firstname.getText().toString();
                String lastname_str = lastname.getText().toString();
                
        		input.put("username", username_str);
        		input.put("password", password_str);
        		input.put("firstname", firstname_str);
        		input.put("lastname", lastname_str);
        		input.put("action", "register.do");
        		try {
        			Map<String, Object> output = MySocket.runSocket(input);
	        	    List<String> errors = (List<String>) output.get("errors");
	        	    if (errors.size()==0) {
	        	    	startActivity(toMain);
	        	    	finish();
	        	    }
	        	    else {
	        	    	Intent toError = new Intent(RegisterActivity.this, ErrorActivity.class);
	        	    	toError.putStringArrayListExtra("errorlist", new ArrayList<String>(errors));
	        	    	startActivity(toError);
	        	    }
				} catch (UnknownHostException e) {
					System.out.println("UnknownHostException");
					e.printStackTrace();
				} catch (IOException e) {
					System.out.println("IOException");
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					System.out.println("ClassNotFoundException");
					e.printStackTrace();
				}        	
        	}
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true; 
    }
}
